package com.rattsoftware.client.controllers;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.rattsoftware.client.RattsAsyncCallback;
import com.rattsoftware.client.RefreshableEvent;
import com.rattsoftware.client.RefreshablePublisher;
import com.rattsoftware.client.webclient;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.CourseSummary;
import com.rattsoftware.client.model.TestData;
import com.rattsoftware.client.service.CourseServiceAsync;
import com.rattsoftware.client.util.ProgressIndicator;
import com.rattsoftware.client.views.CourseView;
import com.rattsoftware.server.admin.model.Course;
import com.rattsoftware.server.admin.model.Editable;
import com.rattsoftware.server.admin.model.User;
import com.rattsoftware.server.admin.model.exceptions.RattsParsingException;

public class CourseController extends Controller{

	private CourseView courseView;
	
	// need a course service here to fetch all courses from the server and fill the grid.
	private CourseServiceAsync courseService;
	
	public CourseController() {
		super();
		registerEventTypes(AppEvents.Init);
		registerEventTypes(AppEvents.CourseView);
		
	}
	
	@Override
	public void handleEvent(AppEvent event) {
		GWT.log("CourseController : handleEvent");
		EventType type = (EventType) event.getType();
		if (type == AppEvents.Init) {
			forwardToView(courseView, event);
		} else if (type == AppEvents.CourseView) {
			forwardToView(courseView, event);
		}
	}

	public void initialize() {
		GWT.log("CourseController : initialize");
		super.initialize();
		courseService = Registry.get(webclient.COURSE_SERVICE);
		courseView = new CourseView(this);
		
		RefreshablePublisher.get().subscribe(RefreshableEvent.Type.CREATE_COURSE, courseView);
		RefreshablePublisher.get().subscribe(RefreshableEvent.Type.UPDATE_COURSE, courseView);
		RefreshablePublisher.get().subscribe(RefreshableEvent.Type.DELETE_COURSE, courseView);
	}
	
	public void forwardToViewCourseUnitController(CourseSummary courseSummary) {
		GWT.log("CourseController : forwardToCourseUnitController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.ViewCourseUnits, courseSummary);
	}
	
	public void forwardToNewCourseController() {
		GWT.log("CourseController : forwardToNewCourseController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.NewCourse);
	}
	
	public void forwardToEditCourseController(CourseSummary courseSummary) {
		GWT.log("CourseController : forwardToEditCourseController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.EditCourse, courseSummary);
	}
	
	public void forwardToNewCourseUnitController(CourseSummary courseSummary) {
		GWT.log("CourseController : forwardToNewCourseUnitController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.NewCourseUnit, courseSummary);
	}
	
	public void getCourses(final PagingLoadConfig pagingLoadConfig, 
			final AsyncCallback<com.extjs.gxt.ui.client.data.PagingLoadResult<CourseSummary>> callback)
			throws RattsParsingException {
		GWT.log("CourseController : getCourses");
		User user = Registry.get(webclient.LOGGED_IN_USER_NAME);
		
		/*courseService.getCourseList(user, PagingUtil.createPagingLoadConfig(pagingLoadConfig),
				new RattsAsyncCallback<PagingLoadResult<Course>>() {
					@Override
					public void onSuccess(PagingLoadResult<Course> result) {*/
						ProgressIndicator.hideProgressBar();
						List<CourseSummary> results = new ArrayList<CourseSummary>();
						//List<Course> data = result.getData();
						List<Course> data = TestData.getCourses();
						
						for (Course f : data) {
							results.add(new CourseSummary(f));
						}
						courseView.setAllCourseSummaries(results); // reset the list
						callback.onSuccess(new BasePagingLoadResult<CourseSummary>(results, 0, 20));
						/*callback.onSuccess(new BasePagingLoadResult<CourseSummary>(results, result.getOffset(), result
								.getTotalLength()));
					}
				});*/
	}
	
	private void saveCourse(Course study, final Editable editable) {
		courseService.saveCourse(study, new RattsAsyncCallback<Course>() {

			@Override
			public void onSuccess(Course result) {
				RefreshablePublisher.get().publish(new RefreshableEvent(RefreshableEvent.Type.DELETE_COURSE, editable));
				//deleteStudyFormView.closeWindow();
			}

		});
	}
	
	public void delete(final Editable editable) {
		if (editable instanceof Course) {

			GWT.log("CourseController : delete Course");
			courseService.deleteCourse((Course) editable, new RattsAsyncCallback<Void>() {

				@Override
				public void onSuccess(Void result) {
					RefreshablePublisher.get().publish(
							new RefreshableEvent(RefreshableEvent.Type.DELETE_COURSE, editable));
					//deleteStudyFormView.closeWindow();
				}
			});
		}
	}

}
